package football.haha.com.footballtv.activity;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashMap;
import java.util.Map;

import football.haha.com.footballtv.R;
import football.haha.com.footballtv.bean.BeanAuthPlayUrl;
import football.haha.com.footballtv.bean.BeanIndexFight;
import football.haha.com.footballtv.bean.BeanUCSResp;
import football.haha.com.footballtv.utils.ApiUtils;
import football.haha.com.footballtv.utils.NetUtils;
import football.haha.com.footballtv.utils.TVToast;
import football.haha.com.footballtv.utils.Tools;
import football.haha.com.footballtv.view.FocusBorderView;

/**
 * Created by haha on 2017/10/3.
 */

public class BaseActivity extends FragmentActivity implements View.OnFocusChangeListener, View.OnClickListener, NetUtils.NetCallBack {


    public FocusBorderView borderView;
    public float scale = 1.1f;
    private String videoName;
    private String contentType;
    private String contentId;
    private String endTime;
    public View focusView = null;
    public long mExitTime = 0;

    //延时设置view获得焦点
    public void showViewByHandler(final View view) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (borderView != null) {
                    borderView.setVisibility(View.VISIBLE);
                }
                view.requestFocus();
            }
        }, 400);
    }

    /**
     * @param rootView 创建边框View
     */
    public void createOnlyBorderView(ViewGroup rootView) {
        borderView = new FocusBorderView(this);
        borderView.setBorderBitmapResId(R.drawable.bg_shape_white);
        borderView.setBorderViewHide();

//        if (rootView != null) {
//            rootView.addView(borderView);
//        }
    }

    /**
     * @param rootView 创建边框View
     */
    public void createBorderView(ViewGroup rootView) {
        borderView = new FocusBorderView(this);
        borderView.setBorderBitmapResId(R.drawable.bg_shape_white);
        borderView.setBorderViewHide();

        traversal(rootView);

//        if (rootView != null) {
//            rootView.addView(borderView);
//        }
    }

    private void traversal(ViewGroup viewGroup) {
        int count = viewGroup.getChildCount();
        for (int i = 0; i < count; i++) {
            View view = viewGroup.getChildAt(i);
            doView(view);
            if (view instanceof ViewGroup)
                traversal((ViewGroup) view);
        }
    }

    /**
     * 处理view
     *
     * @param view
     */
    private void doView(View view) {
        if (view == null)
            return;
        Object tagO = view.getTag();
        if (tagO == null)
            return;
        String tag = tagO.toString();
        if (TextUtils.isEmpty(tag))
            return;
        if (tag.equals(this.getResources().getString(R.string.tagForFocus))) {
            view.setFocusable(true);
            view.setOnFocusChangeListener(this);
        }
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            focusView = v;
            v.bringToFront();
            v.getParent().bringChildToFront(v);
            v.requestLayout();
            v.invalidate();
//            borderView.bringToFront();
//            borderView.setHideView(false);//这一句非常重要
//            borderView.setFocusView(v, scale);
        } else {
//            borderView.setUnFocusView(v);
//            borderView.setHideView(true);
        }

    }

    @Override
    public void onClick(View view) {

    }

    public void showToast(String tip) {
        TVToast.makeText(this, tip, Toast.LENGTH_SHORT).show();
    }

    public void showDialogTip(String tipStr, String leftStr, String rightStr) {
        final Dialog singeLeftRightTipDialog = new Dialog(this, R.style.MyDialog);
        singeLeftRightTipDialog.setContentView(R.layout.dialog_tip);

        TextView tvTip = (TextView) singeLeftRightTipDialog.findViewById(R.id.tv_tip);
        tvTip.setText(tipStr);

        Button left = (Button) singeLeftRightTipDialog.findViewById(R.id.btn_ok);
        left.setText(leftStr);
        singeLeftRightTipDialog.findViewById(R.id.fl_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (singeLeftRightTipDialog != null) {
                    singeLeftRightTipDialog.dismiss();
                }
                clickDialogLeft();
            }
        });

        Button right = (Button) singeLeftRightTipDialog.findViewById(R.id.btn_cancel);
        right.setText(rightStr);
        singeLeftRightTipDialog.findViewById(R.id.fl_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (singeLeftRightTipDialog != null) {
                    singeLeftRightTipDialog.dismiss();
                }
                clickDialogRight();
            }
        });

        RelativeLayout warp = (RelativeLayout) singeLeftRightTipDialog.findViewById(R.id.fl_dialog);
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(Tools.getDisplayWidth(this), Tools.getDisplayHeight(this));
        warp.setLayoutParams(lp);

        singeLeftRightTipDialog.show();

    }

    public void clickDialogLeft() {

    }

    public void clickDialogRight() {

    }

    @Override
    public void netBack(int requestTag, Object object) {
        if (requestTag == ApiUtils.TAGGetAuthPlayUrl) {
            BeanAuthPlayUrl beanAuthPlayUrl = (BeanAuthPlayUrl) object;
            Intent intent = new Intent(this, PlayActivity.class);

            if (!TextUtils.isEmpty(beanAuthPlayUrl.getLiveUrl())) {
                String plauUrl = beanAuthPlayUrl.getLiveUrl();
//                plauUrl = plauUrl.replace("192.168.100.7:8004", "103.251.84.180:2066");
                intent.putExtra("PlayUrl", plauUrl);
                intent.putExtra("videoType", 1);
                showToast("直播地址222：" + beanAuthPlayUrl.getLiveUrl());
            } else if (TextUtils.equals(contentType, "40")) {
                String plauUrl = beanAuthPlayUrl.getLiveUrl();
//                plauUrl = plauUrl.replace("192.168.100.7:8004", "103.251.84.180:2066");
                intent.putExtra("PlayUrl", plauUrl);
                intent.putExtra("videoType", 1);
                showToast("直播地址：" + beanAuthPlayUrl.getLiveUrl());
            } else {
                String plauUrl = beanAuthPlayUrl.getPlayUrl();
//                plauUrl = plauUrl.replace("192.168.100.7:8004", "103.251.84.180:2066");
                intent.putExtra("PlayUrl", plauUrl);
                intent.putExtra("videoType", 0);
                showToast("点播地址：" + beanAuthPlayUrl.getPlayUrl());
            }
            intent.putExtra("videoName", videoName);
            startActivity(intent);

//            Intent intent = new Intent(Intent.ACTION_VIEW);
//            String type = "video/* ";
//            Uri uri = Uri.parse(beanAuthPlayUrl.getPlayUrl());
//            Uri uri = Uri.parse("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");
//            intent.setDataAndType(uri, type);
//            startActivity(intent);
            addHistory(contentId, videoName, "0");
        }
    }

    public void addHistory(String contentId, String contentName, String playExitTime) {
        Map<String, String> params = new HashMap<>();
        params.put("requestType", "1");
        params.put("command", "1");
        params.put("userId", Tools.getDeviceId(this));
        params.put("contentId", contentId);
        params.put("contentName", contentName);
        params.put("playExitTime", playExitTime);

        NetUtils.postJsonData(this, ApiUtils.UCS_URL, ApiUtils.TAGAddHistoryData, params, BeanUCSResp.class, null, this);
    }

    public void getAuthPlayUrl(String videoName, String folderCode, String contentId, String contentType, String endTime) {
        this.videoName = videoName;
        this.contentId = contentId;
        this.contentType = contentType;
        this.endTime = endTime;
        Map<String, String> params = new HashMap<>();
        params.put("folderCode", folderCode);
        params.put("contentId", contentId);
        params.put("contentType", contentType);
        params.put("stbId", Tools.getDeviceId(this));
        params.put("userId", Tools.getDeviceId(this));
        params.put("busCode", "IPS");
        params.put("command", "authPlayUrl");
        NetUtils.getData(this, ApiUtils.HOST_URL, ApiUtils.TAGGetAuthPlayUrl, params, BeanAuthPlayUrl.class, "加载中...", this);
    }

}
